<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Admin;
class AuserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // $auser = new Admin();
        $auser = Admin::orderBy('id','desc')->paginate(2);
        // dd($auser);
        return view('admin.auser_index')->with('auser',$auser);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.auser_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // echo "载入数据库";
        $password = md5($request->password);
        $res=Admin::create(['name'=>$request->name,'password'=>$password,'mobile'=>$request->mobile,'email'=>$request->email]);
        if($res){
            return redirect('/admin/auser');
        }else{
            return back();
        }
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // echo 11111;
        $auser = Admin::where('id',$id)->first();
        return view('admin.auser_edit')->with('user',$auser);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $auser['name']=$request->name;
        $auser['email']=$request->email;
        $auser['mobile']=$request->mobile;
        // $auser['password']=md5($request->password);
        Admin::where('id',$id)->update($auser);
        return redirect('/admin/auser'); 
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    //删除管理员
    public function delUser(Request $request)
    {
        // var_dump($request->id);
        // return DB::table('users')->where('id',$request->id)->delete();
        $res = Admin::where('id',$request->id)->delete();
        return $res;
    }

        //修改密码
    public function modPwd(Request $request,$id)
    {
        // echo $id;
        // dd($request->password);
        $password = md5($request->password);
        Admin::where('id',$id)->update(['password'=>$password]);
        return redirect('admin/auser');
    }

    //修改密码显示页面
    public function showPwd($id)
    {   
        return view('admin.auser_password')->with('id',$id);
    }
}
